{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c83bee7e",
   "metadata": {},
   "source": [
    "# 2.4 微分"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b97c1e5",
   "metadata": {},
   "source": [
    "2.4.1 导数和微分\n",
    "\n",
    "   问题1：为什么要研究导数和微分\n",
    "   \n",
    "    · 在深度学习中，我们通常会选择对于模型参数可微的损失函数\n",
    "    简单说，对于每个参数，我们通过把这个参数增加或减少一个无穷小的量，可以得知损失会以多快的速度增加或减少 \n",
    "    \n",
    "   问题2：导数的定义\n",
    "   \n",
    "    · 𝑓:ℝ→ℝ ，其输入和输出都是标量，则𝑓的导数的定义为𝑓′(𝑥)=lim ℎ→0 [𝑓(𝑥+ℎ)−𝑓(𝑥)]/ℎ\n",
    "     如果此时𝑓′(a)存在，则称 𝑓 在 𝑎 处是可微（differentiable）的。如果 𝑓 在一个区间内的每个数上都是可微的，则此函数在此区间中是可微的。我们可以将 (2.4.1) 中的导数  𝑓′(𝑥)  解释为 𝑓(𝑥) 相对于  𝑥  的 瞬时（instantaneous） 变化率。所谓的瞬时变化率是基于 𝑥 中的变化 ℎ ，且 ℎ 接近 0 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82fe189a",
   "metadata": {},
   "source": [
    "例1：𝑢=𝑓(𝑥)=3𝑥^2−4𝑥 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a6a4ad3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "from IPython import display\n",
    "from d2l import torch as d2l\n",
    "\n",
    "def f(x):\n",
    "    return 3 * x ** 2 - 4 * x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "694d405a",
   "metadata": {},
   "source": [
    "    · 通过令‘𝑥=1’并让‘ℎ’接近0 ，此时‘[𝑓(𝑥+ℎ)−𝑓(𝑥)]/ℎ’的极限无限接近 2。虽然这个实验不是一个数学证明，但我们稍后会看到，当‘𝑥=1’时，导数 𝑢′是 2 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bd607edb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h = 0.10000, numerical limit=2.30000\n",
      "h = 0.01000, numerical limit=2.03000\n",
      "h = 0.00100, numerical limit=2.00300\n",
      "h = 0.00010, numerical limit=2.00030\n",
      "h = 0.00001, numerical limit=2.00003\n"
     ]
    }
   ],
   "source": [
    "def numerical_lim(f, x, h):\n",
    "    return (f(x + h) - f(x)) / h\n",
    "\n",
    "h = 0.1\n",
    "for i in range(5):\n",
    "    print(f'h = {h:.5f}, numerical limit={numerical_lim(f, 1, h):.5f}')\n",
    "    h *= 0.1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ff4c393",
   "metadata": {},
   "source": [
    "    · [补充] 𝑓′(𝑥)=𝑦′=𝑑𝑦/𝑑𝑥=𝑑𝑓/𝑑𝑥=𝑑/𝑑𝑥𝑓(𝑥)=𝐷𝑓(𝑥)=𝐷𝑥𝑓(𝑥) ~~这是常用的导数表达式\n",
    "    · ~~其中符号 𝑑𝑑𝑥 和 𝐷 是微分运算符，表示微分操作。我们可以使用以下规则来对常见函数求微分：\n",
    "    · 𝐷𝐶=0 （𝐶 是一个常数）𝐷𝑥𝑛=𝑛𝑥𝑛−1 （幂律（power rule）, 𝑛是任意实数）𝐷𝑒𝑥=𝑒𝑥 𝐷ln(𝑥)=1/𝑥"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4aab0560",
   "metadata": {},
   "source": [
    "    · 为了使用微分，下面列举了简单的组合函数，下面列举一些常用的运算法则：\n",
    "    ·常数相乘法则     𝑑/𝑑𝑥[𝐶𝑓(𝑥)]=𝐶 𝑑/𝑑𝑥 𝑓(𝑥)\n",
    "    · 加法法则     𝑑/𝑑𝑥[𝑓(𝑥)+𝑔(𝑥)]=𝑑/𝑑𝑥[𝑓(𝑥)]+𝑑/𝑑𝑥[𝑔(𝑥)]\n",
    "    · 乘法法则     𝑑/𝑑𝑥[𝑓(𝑥)𝑔(𝑥)]=𝑓(𝑥)𝑑/𝑑𝑥[𝑔(𝑥)]+𝑔(𝑥)𝑑𝑑𝑥[𝑓(𝑥)]\n",
    "    · 除法法则     𝑑/𝑑𝑥[𝑓(𝑥)𝑔(𝑥)]={ 𝑔(𝑥)𝑑/𝑑𝑥[𝑓(𝑥)]−𝑓(𝑥)𝑑/𝑑𝑥[𝑔(𝑥)] } / [𝑔(𝑥)]2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "979332d9",
   "metadata": {},
   "source": [
    "    · 通过上述给出的公式，不难知道原函数求导后 𝑢′=𝑓′(𝑥)=3𝑑/𝑑𝑥 𝑥^2−4𝑑/𝑑𝑥 𝑥=6𝑥−4\n",
    "    · 当 𝑥=1 时，此导数也是曲线  𝑢=𝑓(𝑥)  切线的斜率，斜率为2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fd4be08",
   "metadata": {},
   "source": [
    "例2：为了使导数可视化，引入matplotlib，在python中是一个流行的绘图库；\n",
    "    \n",
    "    · 在使用之前，我们先定义几个函数，以配置matplotlib生成图像的属性，如下的se_svg_display 函数指定 matplotlib 软件包输出svg图表以获得更清晰的图像；\n",
    "    · 注释’#@save‘是一个特殊的标记，会将对应的函数、类或语句保存在d2l包中 因此，以后无需重新定义就可以直接调用它们；例如，d2l.use_svg_display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "37377ca2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def use_svg_display():  #@save\n",
    "    \"\"\"使用svg格式在Jupyter中显示绘图。\"\"\"\n",
    "    display.set_matplotlib_formats('svg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "536ec5dc",
   "metadata": {},
   "source": [
    "    · 接下来定义set_figsize函数设置图标大小；注意，这里我们直接使用 d2l.plt，因为导入语句from matplotlib import pyplot as plt 已在前言中标记为保存到d2l 包中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ec0f12e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def set_figsize(figsize=(3.5, 2.5)):  #@save\n",
    "    \"\"\"设置matplotlib的图表大小。\"\"\"\n",
    "    use_svg_display()\n",
    "    d2l.plt.rcParams['figure.figsize'] = figsize"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31279c62",
   "metadata": {},
   "source": [
    "    · 下面的set_axes函数用于设置由matplotlib生成图标的轴的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3fe90284",
   "metadata": {},
   "outputs": [],
   "source": [
    "#@save\n",
    "def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):\n",
    "    \"\"\"设置matplotlib的轴\"\"\"\n",
    "    axes.set_xlabel(xlabel)\n",
    "    axes.set_ylabel(ylabel)\n",
    "    axes.set_xscale(xscale)\n",
    "    axes.set_yscale(yscale)\n",
    "    axes.set_xlim(xlim)\n",
    "    axes.set_ylim(ylim)\n",
    "    if legend:\n",
    "        axes.legend(legend)\n",
    "    axes.grid()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d47608f",
   "metadata": {},
   "source": [
    "    · 通过这三个用于图像配置的函数，我们定义了plot函数来简洁地绘制多条曲线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d9d778ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "#@save\n",
    "def plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None,\n",
    "         ylim=None, xscale='linear', yscale='linear',\n",
    "         fmts=('-', 'm--', 'g-.', 'r:'), figsize=(3.5, 2.5), axes=None):\n",
    "    \"\"\"绘制数据点\"\"\"\n",
    "    if legend is None:\n",
    "        legend = []\n",
    "\n",
    "    set_figsize(figsize)\n",
    "    axes = axes if axes else d2l.plt.gca()\n",
    "\n",
    "    # 如果 `X` 有一个轴，输出True\n",
    "    def has_one_axis(X):\n",
    "        return (hasattr(X, \"ndim\") and X.ndim == 1 or\n",
    "                isinstance(X, list) and not hasattr(X[0], \"__len__\"))\n",
    "\n",
    "    if has_one_axis(X):\n",
    "        X = [X]\n",
    "    if Y is None:\n",
    "        X, Y = [[]] * len(X), X\n",
    "    elif has_one_axis(Y):\n",
    "        Y = [Y]\n",
    "    if len(X) != len(Y):\n",
    "        X = X * len(Y)\n",
    "    axes.cla()\n",
    "    for x, y, fmt in zip(X, Y, fmts):\n",
    "        if len(x):\n",
    "            axes.plot(x, y, fmt)\n",
    "        else:\n",
    "            axes.plot(y, fmt)\n",
    "    set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21350100",
   "metadata": {},
   "source": [
    "    · 现在我们可以绘制函数u = 𝑓(𝑥) 及其在x = 1处的切线 y = 2x -3，其中系数2是切线的斜率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7e18b4f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/woyouhenduoyiwen/opt/anaconda3/envs/pytorch/lib/python3.7/site-packages/ipykernel_launcher.py:3: DeprecationWarning: `set_matplotlib_formats` is deprecated since IPython 7.23, directly use `matplotlib_inline.backend_inline.set_matplotlib_formats()`\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<svg height=\"184.15625pt\" version=\"1.1\" viewBox=\"0 0 246.603125 184.15625\" width=\"246.603125pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       " <metadata>\n",
       "  <rdf:RDF xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
       "   <cc:Work>\n",
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
       "    <dc:date>2021-07-04T23:08:57.206162</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.4.2, https://matplotlib.org/</dc:title>\n",
       "     </cc:Agent>\n",
       "    </dc:creator>\n",
       "   </cc:Work>\n",
       "  </rdf:RDF>\n",
       " </metadata>\n",
       " <defs>\n",
       "  <style type=\"text/css\">*{stroke-linecap:butt;stroke-linejoin:round;}</style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 184.15625 \n",
       "L 246.603125 184.15625 \n",
       "L 246.603125 -0 \n",
       "L 0 -0 \n",
       "z\n",
       "\" style=\"fill:none;\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 40.603125 146.6 \n",
       "L 235.903125 146.6 \n",
       "L 235.903125 10.7 \n",
       "L 40.603125 10.7 \n",
       "z\n",
       "\" style=\"fill:#ffffff;\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <path clip-path=\"url(#p4c673ddfb2)\" d=\"M 49.480398 146.6 \n",
       "L 49.480398 10.7 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_2\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 0 3.5 \n",
       "\" id=\"m79b4f09385\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"49.480398\" xlink:href=\"#m79b4f09385\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_3\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 0 -3.5 \n",
       "\" id=\"m36cd43260b\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"49.480398\" xlink:href=\"#m36cd43260b\" y=\"10.7\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <!-- 0 -->\n",
       "      <g transform=\"translate(46.299148 161.198437)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 2034 4250 \n",
       "Q 1547 4250 1301 3770 \n",
       "Q 1056 3291 1056 2328 \n",
       "Q 1056 1369 1301 889 \n",
       "Q 1547 409 2034 409 \n",
       "Q 2525 409 2770 889 \n",
       "Q 3016 1369 3016 2328 \n",
       "Q 3016 3291 2770 3770 \n",
       "Q 2525 4250 2034 4250 \n",
       "z\n",
       "M 2034 4750 \n",
       "Q 2819 4750 3233 4129 \n",
       "Q 3647 3509 3647 2328 \n",
       "Q 3647 1150 3233 529 \n",
       "Q 2819 -91 2034 -91 \n",
       "Q 1250 -91 836 529 \n",
       "Q 422 1150 422 2328 \n",
       "Q 422 3509 836 4129 \n",
       "Q 1250 4750 2034 4750 \n",
       "z\n",
       "\" id=\"DejaVuSans-30\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <path clip-path=\"url(#p4c673ddfb2)\" d=\"M 110.702968 146.6 \n",
       "L 110.702968 10.7 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_5\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"110.702968\" xlink:href=\"#m79b4f09385\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_6\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"110.702968\" xlink:href=\"#m36cd43260b\" y=\"10.7\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <!-- 1 -->\n",
       "      <g transform=\"translate(107.521718 161.198437)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 794 531 \n",
       "L 1825 531 \n",
       "L 1825 4091 \n",
       "L 703 3866 \n",
       "L 703 4441 \n",
       "L 1819 4666 \n",
       "L 2450 4666 \n",
       "L 2450 531 \n",
       "L 3481 531 \n",
       "L 3481 0 \n",
       "L 794 0 \n",
       "L 794 531 \n",
       "z\n",
       "\" id=\"DejaVuSans-31\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <path clip-path=\"url(#p4c673ddfb2)\" d=\"M 171.925539 146.6 \n",
       "L 171.925539 10.7 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_8\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"171.925539\" xlink:href=\"#m79b4f09385\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_9\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"171.925539\" xlink:href=\"#m36cd43260b\" y=\"10.7\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <!-- 2 -->\n",
       "      <g transform=\"translate(168.744289 161.198437)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 1228 531 \n",
       "L 3431 531 \n",
       "L 3431 0 \n",
       "L 469 0 \n",
       "L 469 531 \n",
       "Q 828 903 1448 1529 \n",
       "Q 2069 2156 2228 2338 \n",
       "Q 2531 2678 2651 2914 \n",
       "Q 2772 3150 2772 3378 \n",
       "Q 2772 3750 2511 3984 \n",
       "Q 2250 4219 1831 4219 \n",
       "Q 1534 4219 1204 4116 \n",
       "Q 875 4013 500 3803 \n",
       "L 500 4441 \n",
       "Q 881 4594 1212 4672 \n",
       "Q 1544 4750 1819 4750 \n",
       "Q 2544 4750 2975 4387 \n",
       "Q 3406 4025 3406 3419 \n",
       "Q 3406 3131 3298 2873 \n",
       "Q 3191 2616 2906 2266 \n",
       "Q 2828 2175 2409 1742 \n",
       "Q 1991 1309 1228 531 \n",
       "z\n",
       "\" id=\"DejaVuSans-32\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_10\">\n",
       "      <path clip-path=\"url(#p4c673ddfb2)\" d=\"M 233.148109 146.6 \n",
       "L 233.148109 10.7 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_11\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"233.148109\" xlink:href=\"#m79b4f09385\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_12\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"233.148109\" xlink:href=\"#m36cd43260b\" y=\"10.7\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_4\">\n",
       "      <!-- 3 -->\n",
       "      <g transform=\"translate(229.966859 161.198437)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 2597 2516 \n",
       "Q 3050 2419 3304 2112 \n",
       "Q 3559 1806 3559 1356 \n",
       "Q 3559 666 3084 287 \n",
       "Q 2609 -91 1734 -91 \n",
       "Q 1441 -91 1130 -33 \n",
       "Q 819 25 488 141 \n",
       "L 488 750 \n",
       "Q 750 597 1062 519 \n",
       "Q 1375 441 1716 441 \n",
       "Q 2309 441 2620 675 \n",
       "Q 2931 909 2931 1356 \n",
       "Q 2931 1769 2642 2001 \n",
       "Q 2353 2234 1838 2234 \n",
       "L 1294 2234 \n",
       "L 1294 2753 \n",
       "L 1863 2753 \n",
       "Q 2328 2753 2575 2939 \n",
       "Q 2822 3125 2822 3475 \n",
       "Q 2822 3834 2567 4026 \n",
       "Q 2313 4219 1838 4219 \n",
       "Q 1578 4219 1281 4162 \n",
       "Q 984 4106 628 3988 \n",
       "L 628 4550 \n",
       "Q 988 4650 1302 4700 \n",
       "Q 1616 4750 1894 4750 \n",
       "Q 2613 4750 3031 4423 \n",
       "Q 3450 4097 3450 3541 \n",
       "Q 3450 3153 3228 2886 \n",
       "Q 3006 2619 2597 2516 \n",
       "z\n",
       "\" id=\"DejaVuSans-33\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-33\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_5\">\n",
       "     <!-- x -->\n",
       "     <g transform=\"translate(135.29375 174.876562)scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path d=\"M 3513 3500 \n",
       "L 2247 1797 \n",
       "L 3578 0 \n",
       "L 2900 0 \n",
       "L 1881 1375 \n",
       "L 863 0 \n",
       "L 184 0 \n",
       "L 1544 1831 \n",
       "L 300 3500 \n",
       "L 978 3500 \n",
       "L 1906 2253 \n",
       "L 2834 3500 \n",
       "L 3513 3500 \n",
       "z\n",
       "\" id=\"DejaVuSans-78\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-78\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_13\">\n",
       "      <path clip-path=\"url(#p4c673ddfb2)\" d=\"M 40.603125 118.135514 \n",
       "L 235.903125 118.135514 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_14\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L -3.5 0 \n",
       "\" id=\"m4e4f1b1efd\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"40.603125\" xlink:href=\"#m4e4f1b1efd\" y=\"118.135514\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_15\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 3.5 0 \n",
       "\" id=\"m8ea96b3003\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"235.903125\" xlink:href=\"#m8ea96b3003\" y=\"118.135514\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_6\">\n",
       "      <!-- 0 -->\n",
       "      <g transform=\"translate(27.240625 121.934732)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_16\">\n",
       "      <path clip-path=\"url(#p4c673ddfb2)\" d=\"M 40.603125 80.990157 \n",
       "L 235.903125 80.990157 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_17\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"40.603125\" xlink:href=\"#m4e4f1b1efd\" y=\"80.990157\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_18\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"235.903125\" xlink:href=\"#m8ea96b3003\" y=\"80.990157\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <!-- 5 -->\n",
       "      <g transform=\"translate(27.240625 84.789376)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 691 4666 \n",
       "L 3169 4666 \n",
       "L 3169 4134 \n",
       "L 1269 4134 \n",
       "L 1269 2991 \n",
       "Q 1406 3038 1543 3061 \n",
       "Q 1681 3084 1819 3084 \n",
       "Q 2600 3084 3056 2656 \n",
       "Q 3513 2228 3513 1497 \n",
       "Q 3513 744 3044 326 \n",
       "Q 2575 -91 1722 -91 \n",
       "Q 1428 -91 1123 -41 \n",
       "Q 819 9 494 109 \n",
       "L 494 744 \n",
       "Q 775 591 1075 516 \n",
       "Q 1375 441 1709 441 \n",
       "Q 2250 441 2565 725 \n",
       "Q 2881 1009 2881 1497 \n",
       "Q 2881 1984 2565 2268 \n",
       "Q 2250 2553 1709 2553 \n",
       "Q 1456 2553 1204 2497 \n",
       "Q 953 2441 691 2322 \n",
       "L 691 4666 \n",
       "z\n",
       "\" id=\"DejaVuSans-35\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-35\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_19\">\n",
       "      <path clip-path=\"url(#p4c673ddfb2)\" d=\"M 40.603125 43.844801 \n",
       "L 235.903125 43.844801 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_20\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"40.603125\" xlink:href=\"#m4e4f1b1efd\" y=\"43.844801\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_21\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"235.903125\" xlink:href=\"#m8ea96b3003\" y=\"43.844801\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_8\">\n",
       "      <!-- 10 -->\n",
       "      <g transform=\"translate(20.878125 47.64402)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_9\">\n",
       "     <!-- f(x) -->\n",
       "     <g transform=\"translate(14.798437 87.271094)rotate(-90)scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path d=\"M 2375 4863 \n",
       "L 2375 4384 \n",
       "L 1825 4384 \n",
       "Q 1516 4384 1395 4259 \n",
       "Q 1275 4134 1275 3809 \n",
       "L 1275 3500 \n",
       "L 2222 3500 \n",
       "L 2222 3053 \n",
       "L 1275 3053 \n",
       "L 1275 0 \n",
       "L 697 0 \n",
       "L 697 3053 \n",
       "L 147 3053 \n",
       "L 147 3500 \n",
       "L 697 3500 \n",
       "L 697 3744 \n",
       "Q 697 4328 969 4595 \n",
       "Q 1241 4863 1831 4863 \n",
       "L 2375 4863 \n",
       "z\n",
       "\" id=\"DejaVuSans-66\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 1984 4856 \n",
       "Q 1566 4138 1362 3434 \n",
       "Q 1159 2731 1159 2009 \n",
       "Q 1159 1288 1364 580 \n",
       "Q 1569 -128 1984 -844 \n",
       "L 1484 -844 \n",
       "Q 1016 -109 783 600 \n",
       "Q 550 1309 550 2009 \n",
       "Q 550 2706 781 3412 \n",
       "Q 1013 4119 1484 4856 \n",
       "L 1984 4856 \n",
       "z\n",
       "\" id=\"DejaVuSans-28\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 513 4856 \n",
       "L 1013 4856 \n",
       "Q 1481 4119 1714 3412 \n",
       "Q 1947 2706 1947 2009 \n",
       "Q 1947 1309 1714 600 \n",
       "Q 1481 -109 1013 -844 \n",
       "L 513 -844 \n",
       "Q 928 -128 1133 580 \n",
       "Q 1338 1288 1338 2009 \n",
       "Q 1338 2731 1133 3434 \n",
       "Q 928 4138 513 4856 \n",
       "z\n",
       "\" id=\"DejaVuSans-29\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-66\"/>\n",
       "      <use x=\"35.205078\" xlink:href=\"#DejaVuSans-28\"/>\n",
       "      <use x=\"74.21875\" xlink:href=\"#DejaVuSans-78\"/>\n",
       "      <use x=\"133.398438\" xlink:href=\"#DejaVuSans-29\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_22\">\n",
       "    <path clip-path=\"url(#p4c673ddfb2)\" d=\"M 49.480398 118.135514 \n",
       "L 55.602655 120.88427 \n",
       "L 61.724912 123.187282 \n",
       "L 67.847169 125.04455 \n",
       "L 73.969426 126.456073 \n",
       "L 80.091683 127.421853 \n",
       "L 86.21394 127.941888 \n",
       "L 92.336197 128.016178 \n",
       "L 98.458454 127.644725 \n",
       "L 104.580711 126.827527 \n",
       "L 110.702968 125.564585 \n",
       "L 116.825225 123.855898 \n",
       "L 122.947482 121.701468 \n",
       "L 129.069739 119.101293 \n",
       "L 135.191996 116.055374 \n",
       "L 141.314254 112.56371 \n",
       "L 147.436511 108.626302 \n",
       "L 153.558768 104.24315 \n",
       "L 159.681025 99.414254 \n",
       "L 165.803282 94.139614 \n",
       "L 171.925539 88.419229 \n",
       "L 178.047796 82.2531 \n",
       "L 184.170053 75.641226 \n",
       "L 190.29231 68.583608 \n",
       "L 196.414567 61.080247 \n",
       "L 202.536824 53.13114 \n",
       "L 208.659081 44.73629 \n",
       "L 214.781338 35.895695 \n",
       "L 220.903595 26.609356 \n",
       "L 227.025852 16.877273 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-linecap:square;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"line2d_23\">\n",
       "    <path clip-path=\"url(#p4c673ddfb2)\" d=\"M 49.480398 140.422727 \n",
       "L 55.602655 138.936913 \n",
       "L 61.724912 137.451099 \n",
       "L 67.847169 135.965285 \n",
       "L 73.969426 134.47947 \n",
       "L 80.091683 132.993656 \n",
       "L 86.21394 131.507842 \n",
       "L 92.336197 130.022028 \n",
       "L 98.458454 128.536213 \n",
       "L 104.580711 127.050399 \n",
       "L 110.702968 125.564585 \n",
       "L 116.825225 124.078771 \n",
       "L 122.947482 122.592956 \n",
       "L 129.069739 121.107142 \n",
       "L 135.191996 119.621328 \n",
       "L 141.314254 118.135514 \n",
       "L 147.436511 116.649699 \n",
       "L 153.558768 115.163885 \n",
       "L 159.681025 113.678071 \n",
       "L 165.803282 112.192257 \n",
       "L 171.925539 110.706442 \n",
       "L 178.047796 109.220628 \n",
       "L 184.170053 107.734814 \n",
       "L 190.29231 106.249 \n",
       "L 196.414567 104.763185 \n",
       "L 202.536824 103.277371 \n",
       "L 208.659081 101.791557 \n",
       "L 214.781338 100.305743 \n",
       "L 220.903595 98.819928 \n",
       "L 227.025852 97.334114 \n",
       "\" style=\"fill:none;stroke:#bf00bf;stroke-dasharray:5.55,2.4;stroke-dashoffset:0;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 40.603125 146.6 \n",
       "L 40.603125 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 235.903125 146.6 \n",
       "L 235.903125 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 40.603125 146.6 \n",
       "L 235.903125 146.6 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 40.603125 10.7 \n",
       "L 235.903125 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"legend_1\">\n",
       "    <g id=\"patch_7\">\n",
       "     <path d=\"M 47.603125 48.05625 \n",
       "L 172.153125 48.05625 \n",
       "Q 174.153125 48.05625 174.153125 46.05625 \n",
       "L 174.153125 17.7 \n",
       "Q 174.153125 15.7 172.153125 15.7 \n",
       "L 47.603125 15.7 \n",
       "Q 45.603125 15.7 45.603125 17.7 \n",
       "L 45.603125 46.05625 \n",
       "Q 45.603125 48.05625 47.603125 48.05625 \n",
       "z\n",
       "\" style=\"fill:#ffffff;opacity:0.8;stroke:#cccccc;stroke-linejoin:miter;\"/>\n",
       "    </g>\n",
       "    <g id=\"line2d_24\">\n",
       "     <path d=\"M 49.603125 23.798437 \n",
       "L 69.603125 23.798437 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-linecap:square;stroke-width:1.5;\"/>\n",
       "    </g>\n",
       "    <g id=\"line2d_25\"/>\n",
       "    <g id=\"text_10\">\n",
       "     <!-- f(x) -->\n",
       "     <g transform=\"translate(77.603125 27.298437)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-66\"/>\n",
       "      <use x=\"35.205078\" xlink:href=\"#DejaVuSans-28\"/>\n",
       "      <use x=\"74.21875\" xlink:href=\"#DejaVuSans-78\"/>\n",
       "      <use x=\"133.398438\" xlink:href=\"#DejaVuSans-29\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"line2d_26\">\n",
       "     <path d=\"M 49.603125 38.476562 \n",
       "L 69.603125 38.476562 \n",
       "\" style=\"fill:none;stroke:#bf00bf;stroke-dasharray:5.55,2.4;stroke-dashoffset:0;stroke-width:1.5;\"/>\n",
       "    </g>\n",
       "    <g id=\"line2d_27\"/>\n",
       "    <g id=\"text_11\">\n",
       "     <!-- Tangent line (x=1) -->\n",
       "     <g transform=\"translate(77.603125 41.976562)scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path d=\"M -19 4666 \n",
       "L 3928 4666 \n",
       "L 3928 4134 \n",
       "L 2272 4134 \n",
       "L 2272 0 \n",
       "L 1638 0 \n",
       "L 1638 4134 \n",
       "L -19 4134 \n",
       "L -19 4666 \n",
       "z\n",
       "\" id=\"DejaVuSans-54\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 2194 1759 \n",
       "Q 1497 1759 1228 1600 \n",
       "Q 959 1441 959 1056 \n",
       "Q 959 750 1161 570 \n",
       "Q 1363 391 1709 391 \n",
       "Q 2188 391 2477 730 \n",
       "Q 2766 1069 2766 1631 \n",
       "L 2766 1759 \n",
       "L 2194 1759 \n",
       "z\n",
       "M 3341 1997 \n",
       "L 3341 0 \n",
       "L 2766 0 \n",
       "L 2766 531 \n",
       "Q 2569 213 2275 61 \n",
       "Q 1981 -91 1556 -91 \n",
       "Q 1019 -91 701 211 \n",
       "Q 384 513 384 1019 \n",
       "Q 384 1609 779 1909 \n",
       "Q 1175 2209 1959 2209 \n",
       "L 2766 2209 \n",
       "L 2766 2266 \n",
       "Q 2766 2663 2505 2880 \n",
       "Q 2244 3097 1772 3097 \n",
       "Q 1472 3097 1187 3025 \n",
       "Q 903 2953 641 2809 \n",
       "L 641 3341 \n",
       "Q 956 3463 1253 3523 \n",
       "Q 1550 3584 1831 3584 \n",
       "Q 2591 3584 2966 3190 \n",
       "Q 3341 2797 3341 1997 \n",
       "z\n",
       "\" id=\"DejaVuSans-61\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 3513 2113 \n",
       "L 3513 0 \n",
       "L 2938 0 \n",
       "L 2938 2094 \n",
       "Q 2938 2591 2744 2837 \n",
       "Q 2550 3084 2163 3084 \n",
       "Q 1697 3084 1428 2787 \n",
       "Q 1159 2491 1159 1978 \n",
       "L 1159 0 \n",
       "L 581 0 \n",
       "L 581 3500 \n",
       "L 1159 3500 \n",
       "L 1159 2956 \n",
       "Q 1366 3272 1645 3428 \n",
       "Q 1925 3584 2291 3584 \n",
       "Q 2894 3584 3203 3211 \n",
       "Q 3513 2838 3513 2113 \n",
       "z\n",
       "\" id=\"DejaVuSans-6e\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 2906 1791 \n",
       "Q 2906 2416 2648 2759 \n",
       "Q 2391 3103 1925 3103 \n",
       "Q 1463 3103 1205 2759 \n",
       "Q 947 2416 947 1791 \n",
       "Q 947 1169 1205 825 \n",
       "Q 1463 481 1925 481 \n",
       "Q 2391 481 2648 825 \n",
       "Q 2906 1169 2906 1791 \n",
       "z\n",
       "M 3481 434 \n",
       "Q 3481 -459 3084 -895 \n",
       "Q 2688 -1331 1869 -1331 \n",
       "Q 1566 -1331 1297 -1286 \n",
       "Q 1028 -1241 775 -1147 \n",
       "L 775 -588 \n",
       "Q 1028 -725 1275 -790 \n",
       "Q 1522 -856 1778 -856 \n",
       "Q 2344 -856 2625 -561 \n",
       "Q 2906 -266 2906 331 \n",
       "L 2906 616 \n",
       "Q 2728 306 2450 153 \n",
       "Q 2172 0 1784 0 \n",
       "Q 1141 0 747 490 \n",
       "Q 353 981 353 1791 \n",
       "Q 353 2603 747 3093 \n",
       "Q 1141 3584 1784 3584 \n",
       "Q 2172 3584 2450 3431 \n",
       "Q 2728 3278 2906 2969 \n",
       "L 2906 3500 \n",
       "L 3481 3500 \n",
       "L 3481 434 \n",
       "z\n",
       "\" id=\"DejaVuSans-67\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 3597 1894 \n",
       "L 3597 1613 \n",
       "L 953 1613 \n",
       "Q 991 1019 1311 708 \n",
       "Q 1631 397 2203 397 \n",
       "Q 2534 397 2845 478 \n",
       "Q 3156 559 3463 722 \n",
       "L 3463 178 \n",
       "Q 3153 47 2828 -22 \n",
       "Q 2503 -91 2169 -91 \n",
       "Q 1331 -91 842 396 \n",
       "Q 353 884 353 1716 \n",
       "Q 353 2575 817 3079 \n",
       "Q 1281 3584 2069 3584 \n",
       "Q 2775 3584 3186 3129 \n",
       "Q 3597 2675 3597 1894 \n",
       "z\n",
       "M 3022 2063 \n",
       "Q 3016 2534 2758 2815 \n",
       "Q 2500 3097 2075 3097 \n",
       "Q 1594 3097 1305 2825 \n",
       "Q 1016 2553 972 2059 \n",
       "L 3022 2063 \n",
       "z\n",
       "\" id=\"DejaVuSans-65\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 1172 4494 \n",
       "L 1172 3500 \n",
       "L 2356 3500 \n",
       "L 2356 3053 \n",
       "L 1172 3053 \n",
       "L 1172 1153 \n",
       "Q 1172 725 1289 603 \n",
       "Q 1406 481 1766 481 \n",
       "L 2356 481 \n",
       "L 2356 0 \n",
       "L 1766 0 \n",
       "Q 1100 0 847 248 \n",
       "Q 594 497 594 1153 \n",
       "L 594 3053 \n",
       "L 172 3053 \n",
       "L 172 3500 \n",
       "L 594 3500 \n",
       "L 594 4494 \n",
       "L 1172 4494 \n",
       "z\n",
       "\" id=\"DejaVuSans-74\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-20\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 603 4863 \n",
       "L 1178 4863 \n",
       "L 1178 0 \n",
       "L 603 0 \n",
       "L 603 4863 \n",
       "z\n",
       "\" id=\"DejaVuSans-6c\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 603 3500 \n",
       "L 1178 3500 \n",
       "L 1178 0 \n",
       "L 603 0 \n",
       "L 603 3500 \n",
       "z\n",
       "M 603 4863 \n",
       "L 1178 4863 \n",
       "L 1178 4134 \n",
       "L 603 4134 \n",
       "L 603 4863 \n",
       "z\n",
       "\" id=\"DejaVuSans-69\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 678 2906 \n",
       "L 4684 2906 \n",
       "L 4684 2381 \n",
       "L 678 2381 \n",
       "L 678 2906 \n",
       "z\n",
       "M 678 1631 \n",
       "L 4684 1631 \n",
       "L 4684 1100 \n",
       "L 678 1100 \n",
       "L 678 1631 \n",
       "z\n",
       "\" id=\"DejaVuSans-3d\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-54\"/>\n",
       "      <use x=\"44.583984\" xlink:href=\"#DejaVuSans-61\"/>\n",
       "      <use x=\"105.863281\" xlink:href=\"#DejaVuSans-6e\"/>\n",
       "      <use x=\"169.242188\" xlink:href=\"#DejaVuSans-67\"/>\n",
       "      <use x=\"232.71875\" xlink:href=\"#DejaVuSans-65\"/>\n",
       "      <use x=\"294.242188\" xlink:href=\"#DejaVuSans-6e\"/>\n",
       "      <use x=\"357.621094\" xlink:href=\"#DejaVuSans-74\"/>\n",
       "      <use x=\"396.830078\" xlink:href=\"#DejaVuSans-20\"/>\n",
       "      <use x=\"428.617188\" xlink:href=\"#DejaVuSans-6c\"/>\n",
       "      <use x=\"456.400391\" xlink:href=\"#DejaVuSans-69\"/>\n",
       "      <use x=\"484.183594\" xlink:href=\"#DejaVuSans-6e\"/>\n",
       "      <use x=\"547.5625\" xlink:href=\"#DejaVuSans-65\"/>\n",
       "      <use x=\"609.085938\" xlink:href=\"#DejaVuSans-20\"/>\n",
       "      <use x=\"640.873047\" xlink:href=\"#DejaVuSans-28\"/>\n",
       "      <use x=\"679.886719\" xlink:href=\"#DejaVuSans-78\"/>\n",
       "      <use x=\"739.066406\" xlink:href=\"#DejaVuSans-3d\"/>\n",
       "      <use x=\"822.855469\" xlink:href=\"#DejaVuSans-31\"/>\n",
       "      <use x=\"886.478516\" xlink:href=\"#DejaVuSans-29\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p4c673ddfb2\">\n",
       "   <rect height=\"135.9\" width=\"195.3\" x=\"40.603125\" y=\"10.7\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 252x180 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.arange(0, 3, 0.1)\n",
    "plot(x, [f(x), 2 * x - 3], 'x', 'f(x)', legend=['f(x)', 'Tangent line (x=1)'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15d7b4ee",
   "metadata": {},
   "source": [
    "2.4.2 偏导数\n",
    "\n",
    "   问题1：为什么要引入偏导数\n",
    "    \n",
    "    · 在深度学习中，函数通常依赖于许多变量，因此我们需要将微分的思维推广到这些多远函数上"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16a92aed",
   "metadata": {},
   "source": [
    "    · 设 𝑦=𝑓(𝑥1,𝑥2,…,𝑥𝑛) 是一个具有  𝑛  个变量的函数。 𝑦  关于第 𝑖  个参数 𝑥𝑖 的偏导数（partial derivative）为 ∂𝑦/∂𝑥𝑖 = lim ℎ→0 [𝑓(𝑥1,…,𝑥𝑖−1,𝑥𝑖+ℎ,𝑥𝑖+1,…,𝑥𝑛) − 𝑓(𝑥1,…,𝑥𝑖,…,𝑥𝑛)] / ℎ \n",
    "    · 为了计算∂𝑦/∂𝑥𝑖，我们可以简单地将 𝑥1,…,𝑥𝑖−1,𝑥𝑖+1,…,𝑥𝑛 看作常数，并计算 𝑦关于𝑥𝑖 的导数。对于偏导数的表示，以下是等价的 ∂𝑦∂𝑥𝑖=∂𝑓∂𝑥𝑖=𝑓𝑥𝑖=𝑓𝑖=𝐷𝑖𝑓=𝐷𝑥𝑖𝑓"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca50e68f",
   "metadata": {},
   "source": [
    "2.4.3 梯度\n",
    "\n",
    "   问题1：为什么要引入梯度\n",
    "        \n",
    "    · 比如在预测房价模型时，存在大量的数据需要处理，比较常见的就是n维矩阵，如果直接计算会比较复杂，这个时候需要利用梯度降低算法实现计算的优化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e924cc9",
   "metadata": {},
   "source": [
    "    · 我们可以连结一个多元函数对其所有变量的偏导数，以得到该函数的梯度（gradient）向量。设函数  𝑓:ℝ𝑛→ℝ  的输入是一个 𝑛 维向量  𝐱=[𝑥1,𝑥2,…,𝑥𝑛]⊤ ，并且输出是一个标量。 函数 𝑓(𝐱) 相对于 𝐱 的梯度是一个包含 𝑛 个偏导数的向量: ∇𝐱𝑓(𝐱)=[∂𝑓(𝐱)∂𝑥1,∂𝑓(𝐱)∂𝑥2,…,∂𝑓(𝐱)∂𝑥𝑛]⊤, 其中  ∇𝐱𝑓(𝐱)  通常在没有歧义时被  ∇𝑓(𝐱)  取代。 \n",
    "    · 假设 𝐱 为 𝑛 维向量，在微分多元函数时经常使用以下规则: \n",
    "        · 对于所有 𝐀∈ℝ𝑚×𝑛 ，都有  ∇𝐱𝐀𝐱=𝐀⊤ \n",
    "        · 对于所有 𝐀∈ℝ𝑛×𝑚 ，都有  ∇𝐱𝐱⊤𝐀=𝐀 \n",
    "        · 对于所有 𝐀∈ℝ𝑛×𝑛 ，都有  ∇𝐱𝐱⊤𝐀𝐱=(𝐀+𝐀⊤)𝐱 \n",
    "        · ∇𝐱‖𝐱‖2=∇𝐱𝐱⊤𝐱=2𝐱 \n",
    "    · 同样，对于任何矩阵  𝐗 ，我们都有  ∇𝐗‖𝐗‖2𝐹=2𝐗 。正如我们之后将看到的，梯度对于设计深度学习中的优化算法有很大用处"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6197f04d",
   "metadata": {},
   "source": [
    "2.4.4 链式法则\n",
    "\n",
    "   问题1：为什么要引入链式法则\n",
    "   \n",
    "    · 在深度学习中，多元函数通常是复合（composite）的，所以我们不能直接借助上述的规则来微分这些函数，此时链式法则就出现了"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fc4329e",
   "metadata": {},
   "source": [
    "    · 让我们先考虑单变量函数。假设函数 𝑦=𝑓(𝑢) 和 𝑢=𝑔(𝑥) 都是可微的，根据链式法则：𝑑𝑦𝑑𝑥=𝑑𝑦𝑑𝑢𝑑𝑢𝑑𝑥.\n",
    "    · 对于一个更一般的场景，即函数具有任意数量的变量的情况。假设可微分函数 𝑦 有变量 𝑢1,𝑢2,…,𝑢𝑚，其中每个可微分函数 𝑢𝑖 都有变量 𝑥1,𝑥2,…,𝑥𝑛。注意，𝑦是 𝑥1,𝑥2，…,𝑥𝑛 的函数。对于任意 𝑖=1,2,…,𝑛，链式法则给出：𝑑𝑦𝑑𝑥𝑖=𝑑𝑦𝑑𝑢1𝑑𝑢1𝑑𝑥𝑖+𝑑𝑦𝑑𝑢2𝑑𝑢2𝑑𝑥𝑖+⋯+𝑑𝑦𝑑𝑢𝑚𝑑𝑢𝑚𝑑𝑥𝑖"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2016b600",
   "metadata": {},
   "source": [
    "2.4.5 小结\n",
    "\n",
    "    · 微分和积分是微积分的两个分支，其中前者可以应用于深度学习中无处不在的优化问题。\n",
    "    · 导数可以被解释为函数相对于其变量的瞬时变化率。它也是函数曲线的切线的斜率。\n",
    "    · 梯度是一个向量，其分量是多变量函数相对于其所有变量的偏导数。\n",
    "    · 链式法则使我们能够微分复合函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae96582e",
   "metadata": {},
   "source": [
    "2.4.6 练习\n",
    "    \n",
    "    · 1、绘制函数 𝑦=𝑓(𝑥)=𝑥^3−1/𝑥 和其在 𝑥=1 处切线的图像。\n",
    "    · 2、求函数 𝑓(𝐱)=3𝑥^2,1+5𝑒^𝑥,2 的梯度。 # ^表示取幂，’，‘表示下标\n",
    "    · 3、函数 𝑓(𝐱)=‖𝐱‖,2 的梯度是什么？\n",
    "    · 4、你可以写出函数 𝑢=𝑓(𝑥,𝑦,𝑧) ，其中 𝑥=𝑥(𝑎,𝑏) ， 𝑦=𝑦(𝑎,𝑏) ， 𝑧=𝑧(𝑎,𝑏) 的链式法则吗?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7073f13d",
   "metadata": {},
   "source": [
    "练习1：绘制函数 𝑦=𝑓(𝑥)=𝑥^3−1/𝑥 和其在 𝑥=1 处切线的图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b303d9bf",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/woyouhenduoyiwen/opt/anaconda3/envs/pytorch/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  \n",
      "/Users/woyouhenduoyiwen/opt/anaconda3/envs/pytorch/lib/python3.7/site-packages/ipykernel_launcher.py:3: DeprecationWarning: `set_matplotlib_formats` is deprecated since IPython 7.23, directly use `matplotlib_inline.backend_inline.set_matplotlib_formats()`\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       "  \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<svg height=\"184.15625pt\" version=\"1.1\" viewBox=\"0 0 254.982813 184.15625\" width=\"254.982813pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       " <metadata>\n",
       "  <rdf:RDF xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
       "   <cc:Work>\n",
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
       "    <dc:date>2021-07-04T23:19:39.882683</dc:date>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Matplotlib v3.4.2, https://matplotlib.org/</dc:title>\n",
       "     </cc:Agent>\n",
       "    </dc:creator>\n",
       "   </cc:Work>\n",
       "  </rdf:RDF>\n",
       " </metadata>\n",
       " <defs>\n",
       "  <style type=\"text/css\">*{stroke-linecap:butt;stroke-linejoin:round;}</style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M -0 184.15625 \n",
       "L 254.982813 184.15625 \n",
       "L 254.982813 -0 \n",
       "L -0 -0 \n",
       "z\n",
       "\" style=\"fill:none;\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 48.982813 146.6 \n",
       "L 244.282813 146.6 \n",
       "L 244.282813 10.7 \n",
       "L 48.982813 10.7 \n",
       "z\n",
       "\" style=\"fill:#ffffff;\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <path clip-path=\"url(#p5bf379a3bf)\" d=\"M 57.860085 146.6 \n",
       "L 57.860085 10.7 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_2\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 0 3.5 \n",
       "\" id=\"mf02a558c81\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"57.860085\" xlink:href=\"#mf02a558c81\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_3\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 0 -3.5 \n",
       "\" id=\"m36160be416\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"57.860085\" xlink:href=\"#m36160be416\" y=\"10.7\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <!-- 0 -->\n",
       "      <g transform=\"translate(54.678835 161.198437)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 2034 4250 \n",
       "Q 1547 4250 1301 3770 \n",
       "Q 1056 3291 1056 2328 \n",
       "Q 1056 1369 1301 889 \n",
       "Q 1547 409 2034 409 \n",
       "Q 2525 409 2770 889 \n",
       "Q 3016 1369 3016 2328 \n",
       "Q 3016 3291 2770 3770 \n",
       "Q 2525 4250 2034 4250 \n",
       "z\n",
       "M 2034 4750 \n",
       "Q 2819 4750 3233 4129 \n",
       "Q 3647 3509 3647 2328 \n",
       "Q 3647 1150 3233 529 \n",
       "Q 2819 -91 2034 -91 \n",
       "Q 1250 -91 836 529 \n",
       "Q 422 1150 422 2328 \n",
       "Q 422 3509 836 4129 \n",
       "Q 1250 4750 2034 4750 \n",
       "z\n",
       "\" id=\"DejaVuSans-30\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <path clip-path=\"url(#p5bf379a3bf)\" d=\"M 119.082656 146.6 \n",
       "L 119.082656 10.7 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_5\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"119.082656\" xlink:href=\"#mf02a558c81\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_6\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"119.082656\" xlink:href=\"#m36160be416\" y=\"10.7\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <!-- 1 -->\n",
       "      <g transform=\"translate(115.901406 161.198437)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 794 531 \n",
       "L 1825 531 \n",
       "L 1825 4091 \n",
       "L 703 3866 \n",
       "L 703 4441 \n",
       "L 1819 4666 \n",
       "L 2450 4666 \n",
       "L 2450 531 \n",
       "L 3481 531 \n",
       "L 3481 0 \n",
       "L 794 0 \n",
       "L 794 531 \n",
       "z\n",
       "\" id=\"DejaVuSans-31\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <path clip-path=\"url(#p5bf379a3bf)\" d=\"M 180.305226 146.6 \n",
       "L 180.305226 10.7 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_8\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"180.305226\" xlink:href=\"#mf02a558c81\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_9\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"180.305226\" xlink:href=\"#m36160be416\" y=\"10.7\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <!-- 2 -->\n",
       "      <g transform=\"translate(177.123976 161.198437)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 1228 531 \n",
       "L 3431 531 \n",
       "L 3431 0 \n",
       "L 469 0 \n",
       "L 469 531 \n",
       "Q 828 903 1448 1529 \n",
       "Q 2069 2156 2228 2338 \n",
       "Q 2531 2678 2651 2914 \n",
       "Q 2772 3150 2772 3378 \n",
       "Q 2772 3750 2511 3984 \n",
       "Q 2250 4219 1831 4219 \n",
       "Q 1534 4219 1204 4116 \n",
       "Q 875 4013 500 3803 \n",
       "L 500 4441 \n",
       "Q 881 4594 1212 4672 \n",
       "Q 1544 4750 1819 4750 \n",
       "Q 2544 4750 2975 4387 \n",
       "Q 3406 4025 3406 3419 \n",
       "Q 3406 3131 3298 2873 \n",
       "Q 3191 2616 2906 2266 \n",
       "Q 2828 2175 2409 1742 \n",
       "Q 1991 1309 1228 531 \n",
       "z\n",
       "\" id=\"DejaVuSans-32\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-32\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_10\">\n",
       "      <path clip-path=\"url(#p5bf379a3bf)\" d=\"M 241.527797 146.6 \n",
       "L 241.527797 10.7 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_11\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"241.527797\" xlink:href=\"#mf02a558c81\" y=\"146.6\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_12\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"241.527797\" xlink:href=\"#m36160be416\" y=\"10.7\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_4\">\n",
       "      <!-- 3 -->\n",
       "      <g transform=\"translate(238.346547 161.198437)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 2597 2516 \n",
       "Q 3050 2419 3304 2112 \n",
       "Q 3559 1806 3559 1356 \n",
       "Q 3559 666 3084 287 \n",
       "Q 2609 -91 1734 -91 \n",
       "Q 1441 -91 1130 -33 \n",
       "Q 819 25 488 141 \n",
       "L 488 750 \n",
       "Q 750 597 1062 519 \n",
       "Q 1375 441 1716 441 \n",
       "Q 2309 441 2620 675 \n",
       "Q 2931 909 2931 1356 \n",
       "Q 2931 1769 2642 2001 \n",
       "Q 2353 2234 1838 2234 \n",
       "L 1294 2234 \n",
       "L 1294 2753 \n",
       "L 1863 2753 \n",
       "Q 2328 2753 2575 2939 \n",
       "Q 2822 3125 2822 3475 \n",
       "Q 2822 3834 2567 4026 \n",
       "Q 2313 4219 1838 4219 \n",
       "Q 1578 4219 1281 4162 \n",
       "Q 984 4106 628 3988 \n",
       "L 628 4550 \n",
       "Q 988 4650 1302 4700 \n",
       "Q 1616 4750 1894 4750 \n",
       "Q 2613 4750 3031 4423 \n",
       "Q 3450 4097 3450 3541 \n",
       "Q 3450 3153 3228 2886 \n",
       "Q 3006 2619 2597 2516 \n",
       "z\n",
       "\" id=\"DejaVuSans-33\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-33\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_5\">\n",
       "     <!-- x -->\n",
       "     <g transform=\"translate(143.673438 174.876562)scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path d=\"M 3513 3500 \n",
       "L 2247 1797 \n",
       "L 3578 0 \n",
       "L 2900 0 \n",
       "L 1881 1375 \n",
       "L 863 0 \n",
       "L 184 0 \n",
       "L 1544 1831 \n",
       "L 300 3500 \n",
       "L 978 3500 \n",
       "L 1906 2253 \n",
       "L 2834 3500 \n",
       "L 3513 3500 \n",
       "z\n",
       "\" id=\"DejaVuSans-78\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-78\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_13\">\n",
       "      <path clip-path=\"url(#p5bf379a3bf)\" d=\"M 48.982813 140.426356 \n",
       "L 244.282813 140.426356 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_14\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L -3.5 0 \n",
       "\" id=\"m8df4fdc135\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"48.982813\" xlink:href=\"#m8df4fdc135\" y=\"140.426356\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_15\">\n",
       "      <defs>\n",
       "       <path d=\"M 0 0 \n",
       "L 3.5 0 \n",
       "\" id=\"m538f70ea51\" style=\"stroke:#000000;stroke-width:0.8;\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"244.282813\" xlink:href=\"#m538f70ea51\" y=\"140.426356\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_6\">\n",
       "      <!-- −10 -->\n",
       "      <g transform=\"translate(20.878125 144.225575)scale(0.1 -0.1)\">\n",
       "       <defs>\n",
       "        <path d=\"M 678 2272 \n",
       "L 4684 2272 \n",
       "L 4684 1741 \n",
       "L 678 1741 \n",
       "L 678 2272 \n",
       "z\n",
       "\" id=\"DejaVuSans-2212\" transform=\"scale(0.015625)\"/>\n",
       "       </defs>\n",
       "       <use xlink:href=\"#DejaVuSans-2212\"/>\n",
       "       <use x=\"83.789062\" xlink:href=\"#DejaVuSans-31\"/>\n",
       "       <use x=\"147.412109\" xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_16\">\n",
       "      <path clip-path=\"url(#p5bf379a3bf)\" d=\"M 48.982813 104.135539 \n",
       "L 244.282813 104.135539 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_17\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"48.982813\" xlink:href=\"#m8df4fdc135\" y=\"104.135539\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_18\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"244.282813\" xlink:href=\"#m538f70ea51\" y=\"104.135539\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <!-- 0 -->\n",
       "      <g transform=\"translate(35.620313 107.934758)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_19\">\n",
       "      <path clip-path=\"url(#p5bf379a3bf)\" d=\"M 48.982813 67.844722 \n",
       "L 244.282813 67.844722 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_20\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"48.982813\" xlink:href=\"#m8df4fdc135\" y=\"67.844722\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_21\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"244.282813\" xlink:href=\"#m538f70ea51\" y=\"67.844722\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_8\">\n",
       "      <!-- 10 -->\n",
       "      <g transform=\"translate(29.257813 71.643941)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-31\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_22\">\n",
       "      <path clip-path=\"url(#p5bf379a3bf)\" d=\"M 48.982813 31.553905 \n",
       "L 244.282813 31.553905 \n",
       "\" style=\"fill:none;stroke:#b0b0b0;stroke-linecap:square;stroke-width:0.8;\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_23\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"48.982813\" xlink:href=\"#m8df4fdc135\" y=\"31.553905\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"line2d_24\">\n",
       "      <g>\n",
       "       <use style=\"stroke:#000000;stroke-width:0.8;\" x=\"244.282813\" xlink:href=\"#m538f70ea51\" y=\"31.553905\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_9\">\n",
       "      <!-- 20 -->\n",
       "      <g transform=\"translate(29.257813 35.353124)scale(0.1 -0.1)\">\n",
       "       <use xlink:href=\"#DejaVuSans-32\"/>\n",
       "       <use x=\"63.623047\" xlink:href=\"#DejaVuSans-30\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_10\">\n",
       "     <!-- f(x) -->\n",
       "     <g transform=\"translate(14.798438 87.271094)rotate(-90)scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path d=\"M 2375 4863 \n",
       "L 2375 4384 \n",
       "L 1825 4384 \n",
       "Q 1516 4384 1395 4259 \n",
       "Q 1275 4134 1275 3809 \n",
       "L 1275 3500 \n",
       "L 2222 3500 \n",
       "L 2222 3053 \n",
       "L 1275 3053 \n",
       "L 1275 0 \n",
       "L 697 0 \n",
       "L 697 3053 \n",
       "L 147 3053 \n",
       "L 147 3500 \n",
       "L 697 3500 \n",
       "L 697 3744 \n",
       "Q 697 4328 969 4595 \n",
       "Q 1241 4863 1831 4863 \n",
       "L 2375 4863 \n",
       "z\n",
       "\" id=\"DejaVuSans-66\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 1984 4856 \n",
       "Q 1566 4138 1362 3434 \n",
       "Q 1159 2731 1159 2009 \n",
       "Q 1159 1288 1364 580 \n",
       "Q 1569 -128 1984 -844 \n",
       "L 1484 -844 \n",
       "Q 1016 -109 783 600 \n",
       "Q 550 1309 550 2009 \n",
       "Q 550 2706 781 3412 \n",
       "Q 1013 4119 1484 4856 \n",
       "L 1984 4856 \n",
       "z\n",
       "\" id=\"DejaVuSans-28\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 513 4856 \n",
       "L 1013 4856 \n",
       "Q 1481 4119 1714 3412 \n",
       "Q 1947 2706 1947 2009 \n",
       "Q 1947 1309 1714 600 \n",
       "Q 1481 -109 1013 -844 \n",
       "L 513 -844 \n",
       "Q 928 -128 1133 580 \n",
       "Q 1338 1288 1338 2009 \n",
       "Q 1338 2731 1133 3434 \n",
       "Q 928 4138 513 4856 \n",
       "z\n",
       "\" id=\"DejaVuSans-29\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-66\"/>\n",
       "      <use x=\"35.205078\" xlink:href=\"#DejaVuSans-28\"/>\n",
       "      <use x=\"74.21875\" xlink:href=\"#DejaVuSans-78\"/>\n",
       "      <use x=\"133.398438\" xlink:href=\"#DejaVuSans-29\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_25\">\n",
       "    <path clip-path=\"url(#p5bf379a3bf)\" d=\"M 57.860085 104.135539 \n",
       "L 63.982342 105.478299 \n",
       "L 70.104599 106.603315 \n",
       "L 76.226856 107.510585 \n",
       "L 82.349113 108.200111 \n",
       "L 88.47137 108.671891 \n",
       "L 94.593628 108.925927 \n",
       "L 100.715885 108.962218 \n",
       "L 106.838142 108.780764 \n",
       "L 112.960399 108.381565 \n",
       "L 119.082656 107.764621 \n",
       "L 125.204913 106.929932 \n",
       "L 131.32717 105.877498 \n",
       "L 137.449427 104.60732 \n",
       "L 143.571684 103.119396 \n",
       "L 149.693941 101.413728 \n",
       "L 155.816198 99.490315 \n",
       "L 161.938455 97.349156 \n",
       "L 168.060712 94.990253 \n",
       "L 174.182969 92.413605 \n",
       "L 180.305226 89.619212 \n",
       "L 186.427483 86.607075 \n",
       "L 192.54974 83.377192 \n",
       "L 198.671997 79.929564 \n",
       "L 204.794255 76.264192 \n",
       "L 210.916512 72.381074 \n",
       "L 217.038769 68.280212 \n",
       "L 223.161026 63.961605 \n",
       "L 229.283283 59.425252 \n",
       "L 235.40554 54.671155 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-linecap:square;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"line2d_26\">\n",
       "    <path clip-path=\"url(#p5bf379a3bf)\" d=\"M 63.982342 140.422727 \n",
       "L 70.104599 122.251915 \n",
       "L 76.226856 116.134493 \n",
       "L 82.349113 112.975982 \n",
       "L 88.47137 110.940067 \n",
       "L 94.593628 109.400127 \n",
       "L 100.715885 108.075167 \n",
       "L 106.838142 106.813802 \n",
       "L 112.960399 105.522252 \n",
       "L 119.082656 104.135539 \n",
       "L 125.204913 102.604397 \n",
       "L 131.32717 100.888721 \n",
       "L 137.449427 98.954048 \n",
       "L 143.571684 96.76954 \n",
       "L 149.693941 94.306776 \n",
       "L 155.816198 91.538997 \n",
       "L 161.938455 88.440615 \n",
       "L 168.060712 84.986891 \n",
       "L 174.182969 81.153711 \n",
       "L 180.305226 76.917426 \n",
       "L 186.427483 72.254748 \n",
       "L 192.54974 67.14266 \n",
       "L 198.671997 61.558364 \n",
       "L 204.794255 55.479231 \n",
       "L 210.916512 48.88277 \n",
       "L 217.038769 41.7466 \n",
       "L 223.161026 34.048428 \n",
       "L 229.283283 25.766038 \n",
       "L 235.40554 16.877273 \n",
       "\" style=\"fill:none;stroke:#bf00bf;stroke-dasharray:5.55,2.4;stroke-dashoffset:0;stroke-width:1.5;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 48.982813 146.6 \n",
       "L 48.982813 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 244.282813 146.6 \n",
       "L 244.282813 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_5\">\n",
       "    <path d=\"M 48.982813 146.6 \n",
       "L 244.282813 146.6 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_6\">\n",
       "    <path d=\"M 48.982813 10.7 \n",
       "L 244.282813 10.7 \n",
       "\" style=\"fill:none;stroke:#000000;stroke-linecap:square;stroke-linejoin:miter;stroke-width:0.8;\"/>\n",
       "   </g>\n",
       "   <g id=\"legend_1\">\n",
       "    <g id=\"patch_7\">\n",
       "     <path d=\"M 55.982813 48.05625 \n",
       "L 180.532813 48.05625 \n",
       "Q 182.532813 48.05625 182.532813 46.05625 \n",
       "L 182.532813 17.7 \n",
       "Q 182.532813 15.7 180.532813 15.7 \n",
       "L 55.982813 15.7 \n",
       "Q 53.982813 15.7 53.982813 17.7 \n",
       "L 53.982813 46.05625 \n",
       "Q 53.982813 48.05625 55.982813 48.05625 \n",
       "z\n",
       "\" style=\"fill:#ffffff;opacity:0.8;stroke:#cccccc;stroke-linejoin:miter;\"/>\n",
       "    </g>\n",
       "    <g id=\"line2d_27\">\n",
       "     <path d=\"M 57.982813 23.798437 \n",
       "L 77.982813 23.798437 \n",
       "\" style=\"fill:none;stroke:#1f77b4;stroke-linecap:square;stroke-width:1.5;\"/>\n",
       "    </g>\n",
       "    <g id=\"line2d_28\"/>\n",
       "    <g id=\"text_11\">\n",
       "     <!-- f(x) -->\n",
       "     <g transform=\"translate(85.982813 27.298437)scale(0.1 -0.1)\">\n",
       "      <use xlink:href=\"#DejaVuSans-66\"/>\n",
       "      <use x=\"35.205078\" xlink:href=\"#DejaVuSans-28\"/>\n",
       "      <use x=\"74.21875\" xlink:href=\"#DejaVuSans-78\"/>\n",
       "      <use x=\"133.398438\" xlink:href=\"#DejaVuSans-29\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"line2d_29\">\n",
       "     <path d=\"M 57.982813 38.476562 \n",
       "L 77.982813 38.476562 \n",
       "\" style=\"fill:none;stroke:#bf00bf;stroke-dasharray:5.55,2.4;stroke-dashoffset:0;stroke-width:1.5;\"/>\n",
       "    </g>\n",
       "    <g id=\"line2d_30\"/>\n",
       "    <g id=\"text_12\">\n",
       "     <!-- Tangent line (x=1) -->\n",
       "     <g transform=\"translate(85.982813 41.976562)scale(0.1 -0.1)\">\n",
       "      <defs>\n",
       "       <path d=\"M -19 4666 \n",
       "L 3928 4666 \n",
       "L 3928 4134 \n",
       "L 2272 4134 \n",
       "L 2272 0 \n",
       "L 1638 0 \n",
       "L 1638 4134 \n",
       "L -19 4134 \n",
       "L -19 4666 \n",
       "z\n",
       "\" id=\"DejaVuSans-54\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 2194 1759 \n",
       "Q 1497 1759 1228 1600 \n",
       "Q 959 1441 959 1056 \n",
       "Q 959 750 1161 570 \n",
       "Q 1363 391 1709 391 \n",
       "Q 2188 391 2477 730 \n",
       "Q 2766 1069 2766 1631 \n",
       "L 2766 1759 \n",
       "L 2194 1759 \n",
       "z\n",
       "M 3341 1997 \n",
       "L 3341 0 \n",
       "L 2766 0 \n",
       "L 2766 531 \n",
       "Q 2569 213 2275 61 \n",
       "Q 1981 -91 1556 -91 \n",
       "Q 1019 -91 701 211 \n",
       "Q 384 513 384 1019 \n",
       "Q 384 1609 779 1909 \n",
       "Q 1175 2209 1959 2209 \n",
       "L 2766 2209 \n",
       "L 2766 2266 \n",
       "Q 2766 2663 2505 2880 \n",
       "Q 2244 3097 1772 3097 \n",
       "Q 1472 3097 1187 3025 \n",
       "Q 903 2953 641 2809 \n",
       "L 641 3341 \n",
       "Q 956 3463 1253 3523 \n",
       "Q 1550 3584 1831 3584 \n",
       "Q 2591 3584 2966 3190 \n",
       "Q 3341 2797 3341 1997 \n",
       "z\n",
       "\" id=\"DejaVuSans-61\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 3513 2113 \n",
       "L 3513 0 \n",
       "L 2938 0 \n",
       "L 2938 2094 \n",
       "Q 2938 2591 2744 2837 \n",
       "Q 2550 3084 2163 3084 \n",
       "Q 1697 3084 1428 2787 \n",
       "Q 1159 2491 1159 1978 \n",
       "L 1159 0 \n",
       "L 581 0 \n",
       "L 581 3500 \n",
       "L 1159 3500 \n",
       "L 1159 2956 \n",
       "Q 1366 3272 1645 3428 \n",
       "Q 1925 3584 2291 3584 \n",
       "Q 2894 3584 3203 3211 \n",
       "Q 3513 2838 3513 2113 \n",
       "z\n",
       "\" id=\"DejaVuSans-6e\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 2906 1791 \n",
       "Q 2906 2416 2648 2759 \n",
       "Q 2391 3103 1925 3103 \n",
       "Q 1463 3103 1205 2759 \n",
       "Q 947 2416 947 1791 \n",
       "Q 947 1169 1205 825 \n",
       "Q 1463 481 1925 481 \n",
       "Q 2391 481 2648 825 \n",
       "Q 2906 1169 2906 1791 \n",
       "z\n",
       "M 3481 434 \n",
       "Q 3481 -459 3084 -895 \n",
       "Q 2688 -1331 1869 -1331 \n",
       "Q 1566 -1331 1297 -1286 \n",
       "Q 1028 -1241 775 -1147 \n",
       "L 775 -588 \n",
       "Q 1028 -725 1275 -790 \n",
       "Q 1522 -856 1778 -856 \n",
       "Q 2344 -856 2625 -561 \n",
       "Q 2906 -266 2906 331 \n",
       "L 2906 616 \n",
       "Q 2728 306 2450 153 \n",
       "Q 2172 0 1784 0 \n",
       "Q 1141 0 747 490 \n",
       "Q 353 981 353 1791 \n",
       "Q 353 2603 747 3093 \n",
       "Q 1141 3584 1784 3584 \n",
       "Q 2172 3584 2450 3431 \n",
       "Q 2728 3278 2906 2969 \n",
       "L 2906 3500 \n",
       "L 3481 3500 \n",
       "L 3481 434 \n",
       "z\n",
       "\" id=\"DejaVuSans-67\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 3597 1894 \n",
       "L 3597 1613 \n",
       "L 953 1613 \n",
       "Q 991 1019 1311 708 \n",
       "Q 1631 397 2203 397 \n",
       "Q 2534 397 2845 478 \n",
       "Q 3156 559 3463 722 \n",
       "L 3463 178 \n",
       "Q 3153 47 2828 -22 \n",
       "Q 2503 -91 2169 -91 \n",
       "Q 1331 -91 842 396 \n",
       "Q 353 884 353 1716 \n",
       "Q 353 2575 817 3079 \n",
       "Q 1281 3584 2069 3584 \n",
       "Q 2775 3584 3186 3129 \n",
       "Q 3597 2675 3597 1894 \n",
       "z\n",
       "M 3022 2063 \n",
       "Q 3016 2534 2758 2815 \n",
       "Q 2500 3097 2075 3097 \n",
       "Q 1594 3097 1305 2825 \n",
       "Q 1016 2553 972 2059 \n",
       "L 3022 2063 \n",
       "z\n",
       "\" id=\"DejaVuSans-65\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 1172 4494 \n",
       "L 1172 3500 \n",
       "L 2356 3500 \n",
       "L 2356 3053 \n",
       "L 1172 3053 \n",
       "L 1172 1153 \n",
       "Q 1172 725 1289 603 \n",
       "Q 1406 481 1766 481 \n",
       "L 2356 481 \n",
       "L 2356 0 \n",
       "L 1766 0 \n",
       "Q 1100 0 847 248 \n",
       "Q 594 497 594 1153 \n",
       "L 594 3053 \n",
       "L 172 3053 \n",
       "L 172 3500 \n",
       "L 594 3500 \n",
       "L 594 4494 \n",
       "L 1172 4494 \n",
       "z\n",
       "\" id=\"DejaVuSans-74\" transform=\"scale(0.015625)\"/>\n",
       "       <path id=\"DejaVuSans-20\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 603 4863 \n",
       "L 1178 4863 \n",
       "L 1178 0 \n",
       "L 603 0 \n",
       "L 603 4863 \n",
       "z\n",
       "\" id=\"DejaVuSans-6c\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 603 3500 \n",
       "L 1178 3500 \n",
       "L 1178 0 \n",
       "L 603 0 \n",
       "L 603 3500 \n",
       "z\n",
       "M 603 4863 \n",
       "L 1178 4863 \n",
       "L 1178 4134 \n",
       "L 603 4134 \n",
       "L 603 4863 \n",
       "z\n",
       "\" id=\"DejaVuSans-69\" transform=\"scale(0.015625)\"/>\n",
       "       <path d=\"M 678 2906 \n",
       "L 4684 2906 \n",
       "L 4684 2381 \n",
       "L 678 2381 \n",
       "L 678 2906 \n",
       "z\n",
       "M 678 1631 \n",
       "L 4684 1631 \n",
       "L 4684 1100 \n",
       "L 678 1100 \n",
       "L 678 1631 \n",
       "z\n",
       "\" id=\"DejaVuSans-3d\" transform=\"scale(0.015625)\"/>\n",
       "      </defs>\n",
       "      <use xlink:href=\"#DejaVuSans-54\"/>\n",
       "      <use x=\"44.583984\" xlink:href=\"#DejaVuSans-61\"/>\n",
       "      <use x=\"105.863281\" xlink:href=\"#DejaVuSans-6e\"/>\n",
       "      <use x=\"169.242188\" xlink:href=\"#DejaVuSans-67\"/>\n",
       "      <use x=\"232.71875\" xlink:href=\"#DejaVuSans-65\"/>\n",
       "      <use x=\"294.242188\" xlink:href=\"#DejaVuSans-6e\"/>\n",
       "      <use x=\"357.621094\" xlink:href=\"#DejaVuSans-74\"/>\n",
       "      <use x=\"396.830078\" xlink:href=\"#DejaVuSans-20\"/>\n",
       "      <use x=\"428.617188\" xlink:href=\"#DejaVuSans-6c\"/>\n",
       "      <use x=\"456.400391\" xlink:href=\"#DejaVuSans-69\"/>\n",
       "      <use x=\"484.183594\" xlink:href=\"#DejaVuSans-6e\"/>\n",
       "      <use x=\"547.5625\" xlink:href=\"#DejaVuSans-65\"/>\n",
       "      <use x=\"609.085938\" xlink:href=\"#DejaVuSans-20\"/>\n",
       "      <use x=\"640.873047\" xlink:href=\"#DejaVuSans-28\"/>\n",
       "      <use x=\"679.886719\" xlink:href=\"#DejaVuSans-78\"/>\n",
       "      <use x=\"739.066406\" xlink:href=\"#DejaVuSans-3d\"/>\n",
       "      <use x=\"822.855469\" xlink:href=\"#DejaVuSans-31\"/>\n",
       "      <use x=\"886.478516\" xlink:href=\"#DejaVuSans-29\"/>\n",
       "     </g>\n",
       "    </g>\n",
       "   </g>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p5bf379a3bf\">\n",
       "   <rect height=\"135.9\" width=\"195.3\" x=\"48.982813\" y=\"10.7\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<Figure size 252x180 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.arange(0, 3, 0.1)\n",
    "plot(x, [f(x), x * x * x - 1.0 / x], 'x', 'f(x)', legend=['f(x)', 'Tangent line (x=1)'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a718d5f0",
   "metadata": {},
   "source": [
    "练习2：求函数 𝑓(𝐱)=3𝑥^2,1+5𝑒^𝑥,2 的梯度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "261c1b42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([12.0000, 36.9453])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "import math\n",
    "x1 = 2\n",
    "x2 = 2\n",
    "T_fx = torch.tensor([6*x1, 5*math.exp(x2)])\n",
    "T_fx"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c81e8808",
   "metadata": {},
   "source": [
    "练习3：函数 𝑓(𝐱)=‖𝐱‖,2 的梯度是什么"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "001af477",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  2,  4],\n",
       "        [ 6,  8, 10],\n",
       "        [12, 14, 16],\n",
       "        [18, 20, 22]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ∇𝐱‖𝐱‖2=∇𝐱𝐱⊤𝐱=2𝐱\n",
    "X = torch.arange(12).reshape(4, 3)\n",
    "T2_fx = 2 * X\n",
    "T2_fx"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39279fcf",
   "metadata": {},
   "source": [
    "练习4：你可以写出函数 𝑢=𝑓(𝑥,𝑦,𝑧) ，其中 𝑥=𝑥(𝑎,𝑏) ， 𝑦=𝑦(𝑎,𝑏) ， 𝑧=𝑧(𝑎,𝑏) 的链式法则吗"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e993577d",
   "metadata": {},
   "outputs": [],
   "source": [
    "du/df = f,1*dx/da + f,1*dx/db + f,2*dy/da + f,2*dy/db + f,3*dz/da + f,3*dz/db"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "pytorch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
